package com.lanxigua.ui.user;

import com.lanxigua.entity.User;
import com.lanxigua.ui.Base;
import com.lanxigua.ui.R;
import com.lanxigua.ui.sale.Today;
import com.lanxigua.utils.ErrorException;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Base {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.hideTitle();
		setContentView(R.layout.user_login);

		Button btnLogin = (Button) this.findViewById(R.id.btnLogin);
		btnLogin.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					EditText txtAccount = (EditText) Login.this
							.findViewById(R.id.txtAccount);
					EditText txtPassword = (EditText) Login.this
							.findViewById(R.id.txtPassword);

					Handler handler = new Handler() {
						public void handleMessage(Message message) {
							try {
								User user = new User(message);

								if (user != null) {
									Tooltips.alert(user.name);
									Intent in = new Intent(Login.this,
											Today.class);
									startActivity(in);
									finish();
								}
							} catch (ErrorException e) {
								Tooltips.info(e.getMessage());
							}
						}
					};
					new com.lanxigua.adapter.User(handler).login(txtAccount
							.getText().toString(), txtPassword.getText()
							.toString());
				} catch (Exception e) {
					Tooltips.alert(e.getMessage());
				}
			}
		});
	}
}